﻿using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.CompanySearch.StressTest
{
    public class CompanySearchStressTestRepository
    {        
        private MongoClient Client { get; set; }
        private MongoDatabase Database { get; set; }


        private CompanySearchStressTestRepository(string connectionString)
        {
            // DB address
            MongoUrl mongoUrl = MongoUrl.Create(connectionString);

            if (string.IsNullOrWhiteSpace(mongoUrl.DatabaseName))
            {
                throw new ArgumentException("No database specified in the connection string!");
            }

            // Mongo DB client
            this.Client = new MongoClient(mongoUrl);

            // Mongo DB server
            MongoServer server = this.Client.GetServer();

            // Mongo DB
            this.Database = server.GetDatabase(mongoUrl.DatabaseName);
        }

        public static CompanySearchStressTestRepository GetCompanySearchRepository(string connectionString)
        {
            CompanySearchStressTestRepository repo = new CompanySearchStressTestRepository(connectionString);

            return repo;
        }


        public void Clean()
        {
            // get log entry collection
            var logEntryCol = this.Database.GetCollection<CompanySearchStressTestLogEntry>("CompanySearchStressTestLogEntry");

            // remove all log entries
            logEntryCol.RemoveAll();
        }

        public void AddLogEntry(CompanySearchStressTestLogEntry logEntry)
        {
            // get log entry collection
            var logEntryCol = this.Database.GetCollection<CompanySearchStressTestLogEntry>("CompanySearchStressTestLogEntry");

            // insert log entry
            logEntryCol.Insert(logEntry);
        }
    }
}
